﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace EthernetTools.Utils
{
    class SystemUtils
    {
        [DllImport("SharpWinApiWrapper.dll")]
        private static extern unsafe bool IsWow64(out int LastError);

        /// <summary>
        /// Detect system architecture (x86, x64)
        /// </summary>
        /// <returns>True if version is 64bit</returns>
        /// <exception cref="System.ComponentModel.Win32Exception"> When something wrong. Containing win32 error code</exception>
        public static bool Is64BitOs()
        {
            int lastError = 0;
            bool result = IsWow64(out lastError);

            if (lastError != 0)
            {
                string ERR_STR = "";
                try
                {
                    // cast ulong to LastError can throw an exception
                    ERR_STR = Enum.GetName(typeof(ServiceMangerInternal.ServiceLastError), lastError);
                }
                catch (System.Exception) { }
                throw new System.ComponentModel.Win32Exception(lastError, "Can't get system architecture;" + ERR_STR);
            }

            return result;
        }
    }
}
